<template>
  <div>
    <MBX level1="商品管理" level2="参数列表"></MBX>
    <el-card class="mt16">
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        show-icon
      ></el-alert>
      <!-- 分类 -->
      <div class="cate mt16">
        <p>选择商品分类：</p>
        <el-cascader
          v-model="value"
          :options="options"
          :props="defualt"
          @change="handleChange"
        ></el-cascader>
      </div>
      <!-- 主体 -->
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button
            class="btn"
            type="primary"
            :disabled="btnflag"
            size="mini"
            @click="adddong"
            >添加参数</el-button
          >
          <!-- --------------------------------------添加动态参数 -->
          <!-- table -->
          <el-table :data="donglist" border style="width: 100%">
            <!-- tag -->
            <el-table-column type="expand" width="50">
              <template slot-scope="scope">
                <div>
                  <el-tag
                    v-for="(item, index) in scope.row.attr_vals"
                    :key="index"
                    closable
                    :disable-transitions="false"
                    @close="handleClosedel(scope.row, index)"
                  >
                    {{ item }}
                  </el-tag>
                  <el-input
                    class="input-new-tag"
                    v-if="scope.row.inputVisible"
                    v-model="scope.row.inputValue"
                    ref="saveTagInputdong"
                    size="small"
                    @keyup.enter.native="handleInputConfirm(scope.row)"
                    @blur="handleInputConfirm(scope.row)"
                  >
                  </el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput(scope.row)"
                    >+ New Tag</el-button
                  >
                </div>
              </template>
            </el-table-column>
            <!-- tag -->
            <el-table-column type="index" label="#" width="50">
            </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="160">
              <!-- <template> -->
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="edit(scope.row)"
                  >编辑</el-button
                >
                <el-button type="danger" size="small" @click="del(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <!-- table -->
        </el-tab-pane>

        <el-tab-pane label="静态属性" name="only">
          <el-button
            class="btn"
            type="primary"
            :disabled="btnflag"
            size="mini"
            @click="addjing"
            >添加参数</el-button
          >
          <!-- --------------------------------------添加静态参数 -->
          <!-- table -->
          <el-table :data="jinglist" border style="width: 100%">
            <!-- tag -->
            <el-table-column type="expand" width="50">
              <template slot-scope="scope">
                <div>
                  <el-tag
                    v-for="(item, index) in scope.row.attr_vals"
                    :key="index"
                    closable
                    :disable-transitions="false"
                    @close="handleClosejingdel(scope.row)"
                  >
                    {{ item }}
                  </el-tag>
                  <el-input
                    class="input-new-tag"
                    v-if="scope.row.inputVisiblejing"
                    v-model="scope.row.inputValuejing"
                    ref="saveTagInputjing"
                    size="small"
                    @keyup.enter.native="handleInputConfirmjing(scope.row)"
                    @blur="handleInputConfirmjing(scope.row)"
                  >
                  </el-input>
                  <el-button
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInputjing(scope.row)"
                    >+ New Tag</el-button
                  >
                </div>
              </template>
            </el-table-column>
            <!-- tag -->
            <el-table-column type="index" label="#" width="50">
            </el-table-column>
            <el-table-column prop="attr_name" label="属性名称">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="160">
              <!-- <template> -->
              <template slot-scope="scope">
                <el-button type="primary" size="small" @click="edit(scope.row)"
                  >编辑</el-button
                >
                <el-button type="danger" size="small" @click="del(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <!-- table -->
        </el-tab-pane>
      </el-tabs>
      <!-- 添加动--------- -->
      <el-dialog
        title="添加动态分类"
        :visible.sync="dialogVisibledong"
        width="50%"
      >
        <el-form
          :model="addform"
          :rules="rules"
          ref="addform"
          label-width="100px"
          class="demo-addform"
        >
          <el-form-item label="活动名称" prop="attr_name">
            <el-input v-model="addform.attr_name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addok">立即创建</el-button>
            <el-button @click="dialogVisibledong = false">取 消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

      <!-- 添加jing--------- -->
      <el-dialog
        title="添加静态分类"
        :visible.sync="dialogVisiblejing"
        width="50%"
      >
        <el-form
          :model="addformjing"
          :rules="rules"
          ref="addformjing"
          label-width="100px"
          class="demo-addformjing"
        >
          <el-form-item label="活动名称" prop="attr_name">
            <el-input v-model="addformjing.attr_name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addokjing">立即创建</el-button>
            <el-button @click="dialogVisiblejing = false">取 消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <!-- 编辑dong -->
      <el-dialog title="编辑动态分类" :visible.sync="editvalsble" width="50%">
        <el-form
          :model="editform"
          :rules="rules"
          ref="editform"
          label-width="100px"
          class="demo-editform"
        >
          <el-form-item label="活动名称" prop="attr_name">
            <el-input v-model="editform.attr_name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="editok">立即修改</el-button>
            <el-button @click="editvalsble = false">取 消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import {
  shopcateApi,
  djcateApi,
  editattrApi,
  addattrApi,
  delattrApi
} from '@/http/api'
export default {
  name: 'paramsA',
  data() {
    return {
      // 编辑
      editvalsble: false,
      editform: {
        attr_name: ''
      },
      dialogVisibledong: false,
      addform: {
        attr_name: ''
      },
      rules: {
        attr_name: [
          { required: true, message: '请输入参数名称', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ]
      },
      dialogVisiblejing: false,
      addformjing: {
        attr_name: ''
      },
      defualt: {
        expandTrigger: 'hover',
        value: 'cat_id',
        children: 'children',
        label: 'cat_name'
      },
      value: [],
      options: [],
      //   tab切换的
      activeName: 'many',
      //   btn的disabled
      btnflag: true,
      //   动态参数
      donglist: [],
      //   静态参数
      jinglist: [],
      // -----------动
      // tag标签的数据
      inputValue: '',
      // tag的input与button的显示隐藏
      inputVisible: false,
      // ----------静
      inputVisiblejing: false,
      inputValuejing: ''
    }
  },
  created() {
    this.getcate()
  },
  methods: {
    edit(row) {
      this.editvalsble = true
      this.editform.attr_name = row.attr_name
      this.editform.attr_id = row.attr_id
      this.editform.attr_vals = row.attr_vals
    },
    async editok() {
      await editattrApi(this.lastid, this.editform.attr_id, {
        attr_name: this.editform.attr_name,
        attr_sel: this.activeName,
        attr_vals:
          this.activeName === 'many'
            ? this.editform.attr_vals.join(',')
            : this.editform.attr_vals
      })
      this.handleChange()
      this.editvalsble = false
    },
    del(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await delattrApi(this.lastid, row.attr_id)
          this.handleChange()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 点击tag-----动
    showInput(row) {
      // 点击tag-button控制显示input
      row.inputVisible = true
      this.$nextTick((_) => {
        this.$refs.saveTagInputdong.$refs.input.focus()
      })
    },
    // 失去焦点---动
    async handleInputConfirm(row) {
      // 失去tag-input焦点或者回车---控制显示input
      if (row.inputValue.trim().length > 0) {
        // 失去焦点，push到attr_vals数组中
        row.attr_vals.push(row.inputValue)
        const res = await editattrApi(this.lastid, row.attr_id, {
          attr_name: row.attr_name,
          attr_sel: this.activeName,
          attr_vals:
            this.activeName === 'many' ? row.attr_vals.join(',') : row.attr_vals
        })
        console.log(res)
      }
      row.inputVisible = false
      row.inputValue = ''
    },
    // ------------------------------------------------
    // 点击tag-----静
    showInputjing(row) {
      console.log(row)
      row.inputVisiblejing = true
      this.$nextTick((_) => {
        this.$refs.saveTagInputjing.$refs.input.focus()
      })
    },
    // 失去焦点---静
    async handleInputConfirmjing(row) {
      console.log(row)
      if (row.inputValuejing.trim().length > 0) {
        row.attr_vals.push(row.inputValuejing)
        const res = await editattrApi(this.lastid, row.attr_id, {
          attr_name: row.inputValuejing,
          attr_sel: this.activeName,
          attr_vals: row.attr_vals
        })
        console.log(res)
      }
      this.inputVisiblejing = false
      this.inputValuejing = ''
    },
    //  ----------------------------------------------
    // 获得级联数据
    async getcate() {
      const res = await shopcateApi()
      //   console.log(res)
      this.options = res
    },
    // 切换tab
    handleClick() {
      this.handleChange()
    },
    // 获取列表
    async handleChange() {
      console.log('获得了选中的，在value里')
      //   判断是否有三级，只有选择三级才能添加参数
      if (this.value.length !== 3) {
        this.$message({
          message: '注意：只允许为第三级分类设置相关参数！',
          type: 'error'
        })
        this.btnflag = true
        this.value = ''
        this.donglist = []
        this.jinglist = []
        return false
      }
      this.btnflag = false
      if (this.activeName === 'many') {
        // console.log(this.lastid)
        const res = await djcateApi(this.lastid)
        // console.log(res)
        res.forEach((item) => {
          item.inputValue = ''
          item.inputVisible = false
          item.attr_vals =
            item.attr_vals.length > 0 ? item.attr_vals.split(',') : []
        })
        this.donglist = res
        console.log(this.donglist)
      } else if (this.activeName === 'only') {
        const res = await djcateApi(this.lastid, 'only')
        res.forEach((item) => {
          item.inputValuejing = ''
          item.inputVisiblejing = false
          item.attr_vals =
            item.attr_vals.length > 0 ? item.attr_vals.split(',') : []
        })
        this.jinglist = res
        console.log(this.jinglist)
      }
    },
    // 关闭动态
    async handleClosedel(row, index) {
      console.log(row)
      row.attr_vals.splice(index, 1)
      const res = await editattrApi(this.lastid, row.attr_id, {
        attr_name: row.attr_name,
        attr_sel: this.activeName,
        attr_vals:
          this.activeName === 'many' ? row.attr_vals.join(',') : row.attr_vals
      })
      row = res
      this.handleChange()
    },
    // 关闭静态
    async handleClosejingdel(row, index) {
      // console.log(row)
      // row.attr_vals.splice(index, 1)
      // const res = await editattrApi(this.lastid, row.attr_id, {
      //   attr_name: row.attr_name,
      //   attr_sel: this.activeName,
      //   attr_vals:
      // })
      // console.log(res)
      // this.handleChange()
    },
    // 添加动态
    adddong() {
      this.dialogVisibledong = true
    },
    async addok() {
      this.dialogVisibledong = false
      const res = await addattrApi(this.lastid, {
        attr_name: this.addform.attr_name,
        attr_sel: this.activeName
      })
      console.log(res)
      this.handleChange()
    },
    // 添加静
    addjing() {
      this.dialogVisiblejing = true
    },
    async addokjing() {
      this.dialogVisiblejing = false
      const res = await addattrApi(this.lastid, {
        attr_name: this.addformjing.attr_name,
        attr_sel: this.activeName
      })
      console.log(res)
      this.handleChange()
    }
  },
  computed: {
    //   分类的最后一个id
    lastid() {
      return this.value.at(-1)
    }
  }
}
</script>
<style>
.el-scrollbar__wrap {
  height: 200px;
}
</style>

<style lang="scss" scoped>
.el-cascader {
  width: 300px;
}
.cate {
  display: flex;
  justify-content: left;
  align-items: center;
}
.el-tab-pane {
  text-align: left;
  .btn {
    display: block;
    margin: 20px 0;
  }
}
.el-tag {
  margin-right: 20px;
}
.input-new-tag {
  width: 80px;
}
</style>
